package com.huawei.su;

/**
 * 二分查找  https://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b?tpId=295&tqId=1499549&ru=%2Fpractice%2Fc56f6c70fb3f4849bc56e33ff2a50b6b&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj
 */
public class BM17 {

    public static void main(String[] args) {
    }

    public int search(int[] nums, int target) {
        // write code here
        int left = 0;
        int right = nums.length - 1;
        while (true) {
            if (left > right) {
                break;
            }
            int middle = (left + right) / 2;
            if(nums[middle] == target){
                return middle;
            }else if(nums[middle] > target) {
                right = middle-1;
            }else {
                left = middle+1;
            }
        }
        return -1;
    }
}
